package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.dto.StudyReminderDTO;
import com.example.computer_exam_training_sys.entity.UserStudyReminder;
import com.example.computer_exam_training_sys.mapper.UserStudyReminderMapper;
import com.example.computer_exam_training_sys.service.UserStudyReminderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserStudyReminderServiceImpl implements UserStudyReminderService {

    @Autowired
    private UserStudyReminderMapper reminderMapper;

    @Override
    public List<StudyReminderDTO> getUserReminders(Long userId) {
        List<UserStudyReminder> reminders = reminderMapper.findByUserId(userId);
        return reminders.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public StudyReminderDTO createReminder(StudyReminderDTO reminderDTO) {
        UserStudyReminder reminder = new UserStudyReminder();
        reminder.setUserId(reminderDTO.getUserId());
        reminder.setReminderType(reminderDTO.getReminderType());
        reminder.setReminderTime(reminderDTO.getReminderTime().toLocalTime());
        reminder.setReminderDays(reminderDTO.getReminderDays());
        reminder.setIsActive(true);
        
        reminderMapper.insert(reminder);
        return convertToDTO(reminder);
    }

    @Override
    public StudyReminderDTO updateReminderStatus(Long reminderId, Boolean isActive) {
        reminderMapper.updateStatus(reminderId, isActive);
        UserStudyReminder reminder = reminderMapper.findById(reminderId);
        return convertToDTO(reminder);
    }

    @Override
    public List<StudyReminderDTO> getPendingReminders() {
        List<UserStudyReminder> reminders = reminderMapper.findPendingReminders(LocalDateTime.now());
        return reminders.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<StudyReminderDTO> getTriggeredReminders(Long userId) {
        List<UserStudyReminder> reminders = reminderMapper.findTriggeredReminders(userId, LocalDateTime.now());
        return reminders.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private StudyReminderDTO convertToDTO(UserStudyReminder reminder) {
        StudyReminderDTO dto = new StudyReminderDTO();
        dto.setReminderId(reminder.getReminderId());
        dto.setUserId(reminder.getUserId());
        dto.setReminderType(reminder.getReminderType());
        dto.setReminderTime(reminder.getReminderTime().atDate(LocalDateTime.now().toLocalDate()));
        dto.setReminderDays(reminder.getReminderDays());
        dto.setIsActive(reminder.getIsActive());
        dto.setCreateTime(reminder.getCreateTime());
        dto.setUpdateTime(reminder.getUpdateTime());
        dto.setUserName(reminder.getUserName());
        
        // 设置额外信息
        dto.setStatus(getStatusDescription(reminder));
        dto.setTimeDescription(getTimeDescription(reminder.getReminderTime()));
        dto.setDaysRemaining(calculateDaysRemaining(reminder.getReminderTime()));
        
        return dto;
    }

    private String getStatusDescription(UserStudyReminder reminder) {
        if (reminder == null) {
            return "未知状态";
        }
        
        Boolean isActive = reminder.getIsActive();
        if (isActive == null) {
            return "未知状态";
        }
        
        return isActive ? "已启用" : "已禁用";
    }

    private String getTimeDescription(LocalTime reminderTime) {
        if (reminderTime == null) {
            return "未知时间";
        }
        
        LocalTime now = LocalTime.now();
        if (reminderTime.isBefore(now)) {
            return "今天已过";
        } else if (reminderTime.isAfter(now)) {
            return "今天待提醒";
        } else {
            return "现在";
        }
    }

    private Integer calculateDaysRemaining(LocalTime reminderTime) {
        if (reminderTime == null) {
            return null;
        }
        
        LocalTime now = LocalTime.now();
        if (reminderTime.isBefore(now)) {
            return 0;
        } else {
            return 1;
        }
    }
} 